import javax.xml.soap.Node;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Jun Xu
 * Date：2024/3/9
 * Time: 15:33
 */
public class SingleLinkedList {
    static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
        }
    }

    public Node head;
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
         Node node = new Node(data);
        Node cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = findIndex(index);
        node.next = cur.next;
        cur.next = node;

    }
    private Node findIndex(int index){
        Node cur = head;
        while(index - 1 != 0){
         cur = cur.next;
         index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        //Node head;
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){

    }
    //得到单链表的长度
    public int size(){

    }

    public void display() {

    }

    public void clear() {

    }
}
